package com.youseu.taoliving.commodity.service.impl;

import com.youseu.taoliving.commodity.dao.BrandDao;
import com.youseu.taoliving.commodity.dao.CategoryDao;
import com.youseu.taoliving.commodity.entity.BrandEntity;
import com.youseu.taoliving.commodity.entity.CategoryEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youseu.common.utils.PageUtils;
import com.youseu.common.utils.Query;

import com.youseu.taoliving.commodity.dao.CategoryBrandRelationDao;
import com.youseu.taoliving.commodity.entity.CategoryBrandRelationEntity;
import com.youseu.taoliving.commodity.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Resource
    private BrandDao brandDao;

    @Resource
    private CategoryDao categoryDao;

    @Override
    public void saveAll(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        //接收前端传来的brandId categoryId
        Long brandId = categoryBrandRelationEntity.getBrandId();
        Long categoryId = categoryBrandRelationEntity.getCategoryId();
        //得到brandName categoryName
        BrandEntity brandEntity = brandDao.selectById(brandId);
        categoryBrandRelationEntity.setBrandName(brandEntity.getName());

        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);
        categoryBrandRelationEntity.setCategoryName(categoryEntity.getName());
        //保存信息
        this.save(categoryBrandRelationEntity);

    }

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public List<BrandEntity> getBrandsByCategoryId(Long categoryId) {
        List<CategoryBrandRelationEntity> brandRelationEntities =
                categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", categoryId));

        List<BrandEntity> brandEntities = brandRelationEntities.stream().map((item) -> {
            Long brandId = item.getBrandId();
            return brandDao.selectById(brandId);
        }).collect(Collectors.toList());
        // //得到品牌id
        // List<Long> brandIds = brandRelationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        // //在品牌表查询品牌信息
        // List<BrandEntity> brandEntities = brandDao.selectList(new QueryWrapper<BrandEntity>().in("id", brandIds));
        return brandEntities;
    }

}